package com.example.checkctf.NetworkRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.baselibrary.Base.MyApplication;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.LogUtil;
import com.example.baselibrary.Utils.SPUtils;
import com.example.checkctf.Interface.NetResult;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class NetRequestManger {
    private static NetRequestManger instance;
    private static String BaseUrl;
    private static String GateNumber;

    public static synchronized NetRequestManger getInstance() {
        if (null == instance) {
            instance = new NetRequestManger();
            BaseUrl = (String) SPUtils.get(MyApplication.getAppContext(), Customer.CONFIG_URL, Customer.BASE_URL);
            GateNumber = (String) SPUtils.get(MyApplication.getAppContext(), Customer.CONFIG_GATE_NO, "");

            LogUtil.v("BaseUrl" + BaseUrl);
            LogUtil.v("GateNumber" + GateNumber);
        }
        return instance;
    }

    public void netRequest(String searchDataType, String searchData, int checkCount, String rowKey, String timestamp) {

        // 先封装一个 JSON 对象
        JSONObject param = new JSONObject();
        try {
            param.put("searchDataType", searchDataType);
            param.put("searchData", searchData);
            param.put("checkCount", checkCount);
            param.put("gateNo", GateNumber);
//            param.put("gateNo", "A002002G001");
            param.put("isCollectionGate", "Y");
            param.put("rowKey", rowKey);
            param.put("sign", "A96D48AE69F46BD0E1B17F10316086F3");
            param.put("timestamp", timestamp);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url = BaseUrl + Customer.CHECK_ALL_URL;
        String ss = httpUtils.postJson(url, param);

        ResultManger.getInstance().optimizerResult(ss);
    }

    public void uploadFace(String imgBaseData, String barcodeNo, String certificateName, String certificateNo, String timestamp) {
        // 先封装一个 JSON 对象
        JSONObject param = new JSONObject();
        try {
            param.put("imgBaseData", imgBaseData);
            param.put("barcodeNo", barcodeNo);
            param.put("certificateName", certificateName);
            param.put("certificateNo", "");
            param.put("sign", "A96D48AE69F46BD0E1B17F10316086F3");
            param.put("timestamp", timestamp);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url = BaseUrl + Customer.UPLOAD_FACE_URL;
        String ss = httpUtils.postJson(url, param);

        //根据上传图片返回，进行二次操作
        ResultManger.getInstance().PickFaceResult(ss);

    }

    public void checkFace(String imgBaseData, String timestamp) {
        // 先封装一个 JSON 对象
        JSONObject param = new JSONObject();
//        imgBaseData.replaceAll("\\r", "").replaceAll("\\n", "");

        try {
            param.put("imgBaseData", imgBaseData.replaceAll("\\r", "").replaceAll("\\n", ""));
            param.put("gateNo", GateNumber);
            param.put("sign", "A96D48AE69F46BD0E1B17F10316086F3");
            param.put("timestamp", timestamp);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        String url = BaseUrl + Customer.ONE_FACE_URL;
        String ss = httpUtils.postJson(url, param);

        //1 比 N 人脸对比  结果
        ResultManger.getInstance().checkFaceResult(ss);
    }


}
